# Nonvital bits of bashy2

# This file is part of bashy2, an irc bot used on irc.sdf.org.

# Copyright (C) 2016-2018 mlaine@sdfeu.org

# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#-----------------------------------------------------------------------

function welcome {
    # Sends a help message to a user not in the seen database.
    local db='./data/seen'
    if [[ "$quiet" -eq 1 && "${chan:1}" = '#helpdesk' &&
            -z "$(grep "^$nick " "$db")" ]]; then
        ( cd "$cmd_dir" && './help' "$nick" ) &
    fi
}

function seen {
    # Based on PRIVMSG events, updates a database of seen users. The
    # command seen depends on this database.
    local db='./data/seen'
    if [[ ! -f "$db" ]]; then
        printf '%s\n' "$bot_nick a secret mission /sys/" > "$db"
    fi
    local time="$(date -u +%x\ %R\ %Z)"
    local old="$(grep "^$nick " "$db")"
    local new="$nick $time $chan"
    if [[ -n "$old" ]]; then
        perl -i -pe "s~$old~$new~" "$db"
    else
        printf '%s\n' "$new" >> "$db"
    fi
}

